#define _CRT_SECURE_NO_WARNINGS
#include"sList.h"
SListNode* BuySListNode(SLTDateType x)
{
	SListNode* sl = (SListNode*)malloc(sizeof(SListNode));
	if (sl == NULL)
	{
		perror("malloc");
		return;
	}
	sl->data = x;
	sl->next = NULL;
	return sl;
}

void SListPrint(SListNode* phead)
{
	SListNode* pos= phead;
	while (pos)
	{
		printf("%d->", pos->data);
		pos = pos->next;
	}
	printf("NULL\n");
}

void SListPushBack(SListNode** pplist, SLTDateType x)
{
	assert(pplist);
	if (*pplist == NULL)
	{
		*pplist=BuySListNode(x);
		return;
	}
	SListNode* Node = BuySListNode(x);
	SListNode* pos = *pplist;
	while (pos->next)
	{
		pos = pos->next;
	}
	pos->next = Node;
}

void SListPushFront(SListNode** pplist, SLTDateType x)
{
	assert(pplist);
	if (*pplist == NULL)
	{
		*pplist = BuySListNode(x);
		return;
	}
	SListNode* Node = BuySListNode(x);
	Node->next = *pplist;
	*pplist = Node;
}

void SListPopBack(SListNode** pplist)
{
	assert(*pplist);
	SListNode* pos = *pplist;
	if (pos->next == NULL)
	{
		free(pos);
		*pplist = NULL;
		return;
	}
	while (pos->next->next)
	{
		pos = pos->next;
	}
	free(pos->next);
	pos->next = NULL;
}

void SListPopFront(SListNode** pplist)
{
	assert(*pplist);
	SListNode* pos = *pplist;
	if (pos->next == NULL)
	{
		free(pos);
		*pplist = NULL;
		return;
	}
	pos = pos->next;
	free(*pplist);
	*pplist = pos;
}

SListNode* SListFind(SListNode* plist, SLTDateType x)
{
	SListNode* pos = plist;
	while (pos->data != x && pos->next)
	{
		pos = pos->next;
	}
	if (pos->next == NULL && pos->data!=x)
	{
		return NULL;
	}
	else
	{
		return pos;
	}
}

void SListInsertAfter(SListNode* pos, SLTDateType x)
{
	assert(pos);
	SListNode* Node = BuySListNode(x);
	Node->next = pos->next;
	pos->next = Node;
}

void SListEraseAfter(SListNode* pos)
{
	assert(pos);
	assert(pos->next);
	SListNode* tmp = pos->next->next;
	free(pos->next);
	pos->next = tmp;
}

void SListDestroy(SListNode* plist)
{
	assert(plist);
	SListNode* pos = plist;
	SListNode* tmp = plist->next;
	while (pos->next)
	{
		tmp = pos->next;
		free(pos);
		pos =tmp;
	}
	free(pos);
	pos = tmp = NULL;
}